package easytasks.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Task implements Comparable<Task>, Serializable
{
	private static final long serialVersionUID = 1L;
	private String id;
	private Date time;
	private boolean ignoreHour;
	private String title;
	private String description;
	private List<TaskCategory> categories;
	private boolean done;
	
	public Task(String title, Date time, boolean ignoreHour, String description, boolean done)
	{
		this(null, title, time, ignoreHour, description, done);
	}
	
	public Task(String id, String title, Date time, boolean ignoreHour, String description, boolean done)
	{
		this(id, title, time, ignoreHour, description, new ArrayList<TaskCategory>(), done);
	}
	
	public Task(String title, Date time, boolean ignoreHour, String description, List<TaskCategory> categories, boolean done)
	{
		this(null, title, time, ignoreHour, description, categories, done);
	}
	
	public Task(String id, String title, Date time, boolean ignoreHour, String description, List<TaskCategory> categories, boolean done)
	{
		setId(id);
		setTitle(title);
		setTime(time);
		setIgnoreHour(ignoreHour);
		setDescription(description);
		setCategories(categories);
		markAsDone(done);
	}
	
	public String getId() { return id; }
	public String getTitle() { return title; }
	public Date getTime() { return time; }
	public boolean getIgnoreHour() { return ignoreHour; }
	public String getDescription() { return description; }
	public List<TaskCategory> getCategories() { return categories; }
	public boolean isDone() { return done; }
	
	public void setId(String value)
	{
		if (value == null) value = "";	
		this.id = value.trim();
	}
	
	public void setTitle(String value)
	{
		if (value == null || value.trim().equals("")) throw new IllegalArgumentException("'value' must be not null and not empty.");
		this.title = value.trim();
	}
	
	public void setTime(Date value)
	{
		if (value == null) throw new IllegalArgumentException("'value' must be not null and not empty.");
		this.time = value;
	}
	
	public void setIgnoreHour(boolean ignore)
	{
		this.ignoreHour = ignore;
	}
	
	public void setDescription(String value)
	{
		if (value == null) this.description = "";
		else this.description = value.trim();
	}
	
	public void setCategories(List<TaskCategory> value)
	{
		if (value == null) throw new IllegalArgumentException("'value' must be not null and not empty.");
		this.categories = value;
	}
	
	public void markAsDone(boolean done)
	{
		this.done = done;
	}
	
	@Override
	public boolean equals(Object o)
	{
		boolean equals = false;
		
		if (o != null && o instanceof Task)
		{
			Task other = (Task)o;
			equals = this.id.equals(other.id);
		}
		
		return equals;
	}
	
	public int compareTo(Task other) 
	{
		int result = 1;	
		if (other != null) result = this.time.compareTo(other.time);
		
		return result;
	}
	
	@Override
	public String toString()
	{
		return title;
	}
}
